const express = require("express");
const userRouter = require("./routers/usersRouter");
const loginRouter = require("./routers/loginRouter");
const advRouter = require("./routers/advRouter");
const bodyParser = require("body-parser");
const { expressjwt } = require("express-jwt");

// 系统默认管理者账号
const adminModel = require("./models/adminModel");
const md5 = require("md5");
adminModel.find().then(res=>{
    if (res.length === 0) {
        adminModel.create({username:"admin",pwd:md5("123")})
    }
})

const fs = require("fs");

let app = express();
const static = express.static;
app.use(static("./static"));
app.use(bodyParser.json());
app.use(expressjwt({secret:"mytoken",algorithms:['HS256']}).unless({path:[/^\/login\//,/\.png$/] }))

if (!fs.existsSync("./static")) {
    fs.mkdirSync("./static");
}

app.use("/admin",userRouter)
app.use("/login",loginRouter);
app.use("/adv",advRouter)


app.listen(8989);